/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Assignment.DAO;

import Assignment.DTO.Book_OrderDTO;
import Assignment.DTO.OrderDTO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author dieptn
 */
public class Book_OrderDAO {

    public static ArrayList<Book_OrderDTO> search(Connection con, Book_OrderDTO item) {
        ArrayList<Book_OrderDTO> result = new ArrayList<Book_OrderDTO>();
        String sql = "";
        PreparedStatement stm = null;
        try {
            if (item == null) {
                sql = "SELECT * FROM Book_Order";
                stm = con.prepareStatement(sql);
            }
            ResultSet rs = stm.executeQuery();
            Book_OrderDTO boDTO = null;
            while (rs.next()) {
                boDTO = new Book_OrderDTO();
                boDTO.setOrderID(rs.getString(1));
                boDTO.setBookID(rs.getString(2));
                boDTO.setQuantity(rs.getInt(3));
                result.add(boDTO);
            }
            stm.close();
            con.close();
        } catch (Exception ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public static int insert(Connection con, Book_OrderDTO item) {
        int result = 0;
        try {
            String sql = "INSERT INTO Book_Order VALUES(?,?,?)";
            PreparedStatement stm = con.prepareStatement(sql);
            stm.setString(1, item.getOrderID());
            stm.setString(2, item.getBookID());
            stm.setInt(3, item.getQuantity());
            result = stm.executeUpdate();

            stm.close();
            //con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static int delete(Connection con, String orderID) {
        int result = 0;
        try {
            String sql = "DELETE FROM Book_Order WHERE orderID=?";
            PreparedStatement stm = con.prepareStatement(sql);
            stm.setString(1, orderID);
            result = stm.executeUpdate();
            stm.close();
            //con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
